﻿using AdminAuthority.Attributes;
using AdminAuthority.Entities;

using Furion;
using Furion.DynamicApiController;
using Furion.FriendlyException;

using Mapster;

using Microsoft.AspNetCore.Mvc;

using System;
using System.Collections.Generic;
using System.Linq;

using Tool;

namespace AdminAuthority
{
    public class AuthorityHelper
    {
        /// <summary>
        /// 获取MVC菜单
        /// </summary>
        /// <returns></returns>
        public static List<AuthorityPermissionItem> GetMvcPermissions()
        {
            var types = App.Assemblies.SelectMany(o => o.GetTypes().AsEnumerable()
                                               .Where(type => typeof(ControllerBase).IsAssignableFrom(type)));
            return GetItems(types, "Admin");
        }

        /// <summary>
        /// 获取API菜单
        /// </summary>
        /// <returns></returns>
        public static List<AuthorityPermissionItem> GetApiPermissions()
        {
            var types = App.Assemblies.SelectMany(a => a.GetTypes()
                           .Where(t => t.GetInterfaces().Contains(typeof(IDynamicApiController))));
            return GetItems(types, "Api");
        }

        private static List<AuthorityPermissionItem> GetItems(IEnumerable<Type> types, string menutype)
        {
            var permissions = new List<AuthorityPermissionItem>();

            foreach (var controller in types)
            {
                //var attribute = controller.GetCustomAttributes().FirstOrDefault(o => o is AuthorityControllerAttribute);
                //AuthorityControllerAttribute authorityControllerAttribute = null;
                //if (attribute != null)
                //{
                //    authorityControllerAttribute = attribute as AuthorityControllerAttribute;
                //}
                var controller1 = controller.Name.RemoveLastStr("Controller");// 移除Controller控制器名称
                                                                              // var controllerName = controller1.ToString();
                AuthorityControllerAttribute authorityController = null;
                foreach (var customAttribute in controller.GetCustomAttributes().Where(o => o is AuthorityControllerAttribute))
                {
                    authorityController = (customAttribute as AuthorityControllerAttribute);
                    break;
                }

                foreach (var methodInfo in controller.GetMethods())
                {
                    //获取贴的 权限特性
                    foreach (AuthorityPermissionAttribute actionAttribute in methodInfo.GetCustomAttributes().Where(o => o is AuthorityPermissionAttribute))
                    {
                        var afitem = actionAttribute.Adapt<AuthorityPermissionItem>();
                        if (authorityController != null)
                        {
                            afitem.TypeCode = authorityController.TypeCode;
                            if (!string.IsNullOrWhiteSpace(afitem.Controller))
                            {
                                afitem.Controller = authorityController.Controller;
                            }
                            else
                            {
                                afitem.Controller = controller1;
                            }
                            if (!string.IsNullOrWhiteSpace(authorityController.ControllerDescription))
                            {
                                afitem.ControllerDescription = authorityController.ControllerDescription;
                            }
                            else
                            {
                                afitem.ControllerDescription = controller1;
                            }

                            //afitem.Controller=
                        }
                        else
                        {
                            afitem.Controller = controller1;
                            afitem.ControllerDescription = controller1;
                        }
                        //afitem.ControllerName = controllerName;

                        //if (string.IsNullOrWhiteSpace(afitem.Controller))
                        //{
                        //    afitem.Controller = controller1;// controller.Name.RemoveLastStr("Controller");// 移除Controller控制器名称
                        //}

                        if (permissions.Any(o => o.Controller.ToLower().Trim() == afitem.Controller.ToLower().Trim() && o.Actions.ToLower().Trim() == afitem.Actions.ToLower().Trim()))
                        {
                            Oops.Oh(errorMessage: $"获取权限时候 {controller.Name} 中 {afitem.Actions} 出现重复");
                        }

                        permissions.Add(afitem);
                    }
                }
            }
            return permissions;
        }
    }
}